﻿using System;
using System.IO;
using System.Web.Mvc;

namespace ProjetoTcc.Controllers {
    public class TccController : Controller {
        public string Cont = "";

        //// 
        // GET: /Tcc/

        public ActionResult Pagina()
        {
            var path = new DirectoryInfo(Server.MapPath("~/Tcc/"));
            if (!Directory.Exists(path + "teste"))
            {
                Directory.CreateDirectory(path + "teste");
                if (!System.IO.File.Exists(path + "teste" + "/ArquivoExemplo.txt"))
                    using (System.IO.File.Create(path + "teste" + "/ArquivoExemplo.txt")) { }
            }
            else
            {
                using (var fx = new StreamReader(path + "teste" + "/ArquivoExemplo.txt"))
                {
                    string linha;
                    while ((linha = fx.ReadLine()) != null)
                    {
                        //  nome = collection["nome"].ToString();
                        Cont += linha;
                    }
                    ViewData["teste"] = Cont;
                }
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Salvar(FormCollection conteudo)
        {
            var path = new DirectoryInfo(Server.MapPath("~/Tcc/"));
            try
            {
                using (var sc = new StreamWriter(path + "teste" + "/ArquivoExemplo.txt"))
                {
                    sc.Write(conteudo["editor"]);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.StackTrace);
            }
            return RedirectToAction("Pagina");
        }

       /* [HttpPost]
        public ActionResult Pagina(FormCollection collection){
         //   string nome = "";
            try
            {
                if (System.IO.File.Exists(strPathFile))
                {
                    using (StreamReader fx = new StreamReader(strPathFile))
                    {
                        string cont = "";
                        string linha = "";
                        while ((linha = fx.ReadLine()) != null)
                        {
                            //  nome = collection["nome"].ToString();
                            cont += linha;
                        }
                       
                        ViewData["text"] = cont;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.StackTrace);
            }
            /* 
             * Criar
             * try {
                 using (System.IO.FileStream fs = System.IO.File.Create(strPathFile)) {
                     using (StreamWriter sw = new StreamWriter(fs)) {
                         sw.Write(collection["nome"].ToString());
                     }
                       nome = collection["nome"].ToString();
                        ViewData["text"] = nome;
                 }
            
             }
             catch(Exception ex){
                 Console.Write(ex.StackTrace);
             }

             string nome = "";
             nome = collection["nome"].ToString();
             ViewData["text"] = nome;*/
          //  return View();
       // }
    }
}